﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Android.App;
using Android.Widget;
using Android.Views;
using Android.Content;
using LinkNote.Model;
using Android.Graphics;
using Path = System.IO.Path;

namespace LinkNote.Core
{
    public class AttachmentView
    {
        private Context context;
        private ViewGroup parent;

        public event DeleteClickDelegate<AttachmentBlock> DeleteClick;
        private AttachmentBlock data;
        private ImageButton btnClose;

        public AttachmentView(Context context, ViewGroup parent, AttachmentBlock fileName)
        {
            this.context = context;
            this.parent = parent;
            this.data = fileName;

            LayoutInflater li = LayoutInflater.FromContext(context);
            View v = li.Inflate(Resource.Layout.attachment, parent, false);

            //Get references to the child controls
            ImageView contentView = v.FindViewById<ImageView>(Resource.Id.contentView);
            btnClose = v.FindViewById<ImageButton>(Resource.Id.btnClose);
            btnClose.Click += (sender, e) =>
            {
                parent.RemoveView(v);
                DeleteClick(sender, data);
            };
            parent.AddView(v);

            if (string.IsNullOrEmpty(data.file) || !System.IO.File.Exists(data.file))
                CoreHelper.DownLoadFile(data, delegate { LoadLocalFile(contentView); });
            else
                LoadLocalFile(contentView);

            contentView.Click += (s, e) =>
                {
                    var dlg = new PreviewPictureDialog(context, data.file);
                    dlg.Show();
                };
        }

        private void LoadLocalFile(ImageView contentView)
        {
            if (data.type == AttachmentType.Picture)
            {
                BitmapFactory.Options op = new BitmapFactory.Options();
                op.InSampleSize = 2;
                using (Bitmap bitmap = BitmapFactory.DecodeFile(data.file, op))
                {
                    contentView.SetImageBitmap(bitmap);
                    GC.Collect();
                }
            }
        }

        public bool Editable
        {
            get { return btnClose.Visibility == ViewStates.Visible; }
            set
            {
                btnClose.Visibility = value ? ViewStates.Visible : ViewStates.Invisible;
            }
        }
    }

    public class PreviewPictureDialog : Dialog
    {
        public PreviewPictureDialog(Context context, string localFile)
            : base(context)
        {
            ImageView vv = new ImageView(context);
            ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
            this.AddContentView(vv, param);

            BitmapFactory.Options op = new BitmapFactory.Options();
            op.InSampleSize = 2;
            using (Bitmap bitmap = BitmapFactory.DecodeFile(localFile, op))
            {
                vv.SetImageBitmap(bitmap);
                GC.Collect();
            }
        }
    }
}
